package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.ecs.model.v20140526.DescribeNewProjectEipMonitorDataRequest;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.entity.ApArticle;
import com.heima.article.service.IApArticleService;
import com.heima.article.service.IHotArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class HotArticleServiceImpl implements IHotArticleService {

    @Autowired
    IApArticleService apArticleService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void compute() {

        Date now = new Date();

        Date to = new Date(now.getYear(), now.getMonth(), now.getDate());

        Date from = new Date(to.getTime() - 5 * 60 * 60 * 24 * 1000);

        LambdaQueryWrapper<ApArticle> query = new LambdaQueryWrapper<>();

        query.gt(ApArticle::getPublishTime, from).lt(ApArticle::getPublishTime, to);

        List<ApArticle> list = apArticleService.list(query);

        for (ApArticle article : list) {

            // 计算文章分值
            double score = computeScore(article);
            // 将文章数据和分值缓存到redis中
            // 将文章不变的字段保存(页面需要的字段)
            ApArticle articleToCache = new ApArticle();
            articleToCache.setId(article.getId());
            articleToCache.setTitle(article.getTitle());
            articleToCache.setAuthorId(article.getAuthorId());
            articleToCache.setAuthorName(article.getAuthorName());
            articleToCache.setImages(article.getImages());
            articleToCache.setChannelId(article.getChannelId());
            articleToCache.setChannelName(article.getChannelName());
            articleToCache.setPublishTime(article.getPublishTime());

            if (score >= 0) {

                String firstkey = "hot_article_first_page_0";

                String value = JSON.toJSONString(articleToCache);

                redisTemplate.opsForZSet().add(firstkey, value, score);

                redisTemplate.expire(firstkey, 23, TimeUnit.HOURS);

                String channelKey = "hot_article_first_page_" + article.getChannelId();

                redisTemplate.opsForZSet().add(channelKey, value, score);

                redisTemplate.expire(channelKey, 23, TimeUnit.HOURS);
            }
        }

    }

    private double computeScore(ApArticle article) {

        double score = 0;

        if (article.getViews() != null) {

            score += article.getViews() * 1;
        }
        if (article.getLikes() != null) {
            score += article.getLikes() * 3;
        }
        if (article.getComment() != null) {
            score += article.getComment() * 5;
        }
        if (article.getCollection() != null) {
            score += article.getCollection() * 8;
        }
        return score;
    }
}
